from core.cell_data_handler import CellDataHandler
from typing import Callable
from random import randint


class RandomValueCellDataHandler(CellDataHandler):

    __slots__ = [
        'values',
        'index_callback'
    ]

    def __init__(self, values: tuple, index_callback: Callable[[tuple], int] or None):
        self.values = values
        self.index_callback = index_callback if index_callback is not None else \
            lambda t: randint(0, len(self.values) - 1)

    def handle(self, row_data: tuple):
        index = self.index_callback(row_data)
        if index < 0 or index >= len(self.values):
            raise ValueError('随机索引越界')
        return self.values[index]
